import java.io.*;
import java.net.Socket;
import java.util.Scanner;

public class ClientThread extends Thread {
    private Socket socket;
    public Socket getSocket() {
        return socket;
    }
    public void setSocket(Socket socket) {
        this.socket = socket;
    }
    public ClientThread(Socket socket) {
        super();
        this.socket = socket;
    }
    @Override
    public void run() {
        System.out.println("请输入聊天内容：");
        Scanner sc = new Scanner(System.in);
        OutputStream oo=null;
        try {
            //将输入的聊天内容放到输出流中
            oo=socket.getOutputStream();
            BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(oo));
            while(true){
                String content = sc.nextLine().trim();
                //如果输入内容包含file文件，就发送文件给服务器
                if(content.contains("file")){
                    //创建FileInputStream对象实例，将要发送的文件写入到输出流数据缓冲区中
                    File file=new File("E:/dd/a.txt");
                    FileInputStream fis = new FileInputStream(file);
                    System.out.println(file.getName());

                    byte[] b=new byte[1024];
                    int len;
                    while((len=fis.read(b)) !=-1 ){
                        System.out.println(len=fis.read(b));
                        oo.write(b, 0, len);
                    }
                    //告知服务器端我发送的数据完毕。
                    socket.shutdownOutput();
                }else {
                    bw.write(content);
                    bw.newLine();//换行
                    bw.flush();//刷新缓冲流，将数据写到文件里
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}